<?php
declare (strict_types=1);

namespace app\admin\service\system;

use app\model\system\SystemGroup;
use app\model\system\SystemRule;

class MenuService
{
    /**
     * 获取用户当前的菜单/权限 列表
     * @param $user
     * @return \think\Collection
     * @throws \Exception
     * @author dgc<453342213@qq.com>
     */
    public function menuArr($user)
    {
        $where[] = ['status', '=', 1];
        if ($user['group_id'] != 1) {
            $rules=(new SystemGroup())->where('id',$user['group_id'])->value('rules','');
            $where[]=['id','in',$user];
        }
        $list = (new SystemRule())->getList($where);
        if (empty($list)) {
            throw  new \Exception('请联系管理员，还未设置权限', -1);
        }
        return $list;
    }

    /**
     * 获取列表
     * @param array $where
     * @return \think\Collection
     * @throws \Exception
     * @author dgc<453342213@qq.com>
     */
    public function getList($where = [])
    {
        $list = (new SystemRule())->getList($where);
        return $list;
    }

    /**
     * 获取详情
     * @param array $where
     * @return \think\Collection
     * @throws \Exception
     * @author dgc<453342213@qq.com>
     */
    public function getInfo($where = [])
    {
        $info = (new SystemRule())->getInfo($where);
        if (is_null($info)) {
            throw  new \Exception('异常操作', -1);
        }
        return $info;
    }

    /**
     * 获取详情
     * @param array $where
     * @return \think\Collection
     * @throws \Exception
     * @author dgc<453342213@qq.com>
     */
    public function saves($param = [], $id)
    {
        $where = [];
        if ($id > 0) {
            $where['id'] = $id;
        }
        $info = (new SystemRule())->where($where)->save($param);
        return $info;
    }

}
